package com.spoto.oa.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * 
 * @Project_name: OA
 * @Description: 封装数据库的基本操作
 * @Author: LinYiSong
 * @Version: 
 * @Date: 2010-9-23~2010-9-23
 */
public class DB {

	/* 连接数据库的信息 */
	private static Properties file;
	static Connection con;
	static Statement stm;
	static ResultSet rs;

	/**
	 * 加载配置文件
	 */
	static {
		file = new Properties();
		try {
			file.load(DB.class.getClassLoader().getResourceAsStream("db.properties"));
		} catch (FileNotFoundException e) {
			System.out.println("没找到配置文件");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 连接数据库
	 * 
	 * @return
	 */
	public static Connection getConn() {

		/* 连接数据库信息 */
		String driver;
		String url;
		String user;
		String password;

		driver = file.getProperty("driver");
		url = file.getProperty("url");
		user = file.getProperty("user");
		password = file.getProperty("password");
	
		try {
			Class.forName(driver);
			return DriverManager.getConnection(url, user, password);
		} catch (ClassNotFoundException e) {
			System.out.println("数据库驱动加载不成功");
		} catch (SQLException e) {
			System.out.println("数据库连接不成功");
		} 
		return null;

	}

	

	/**
	 * 
	 * 关闭数据库
	 */
	public static void close() {
		try {
			if (null != rs) {
				rs.close();

			}
			if (null != stm) {
				stm.close();
			}
			if (null != con) {
				con.close();
			}
		} catch (SQLException e) {
			System.out.println("数据关闭出错");
		}
	}

}

